const express=require('express')
const router=express.Router()
const connection=require('../../util/request')
const jwtToekn=require('../../util/jwtToken')
const tokenPo=require('../../constant/tokenPo')
let data={}
//用户登录
router.post('/user/login',(req,resp,next)=>{
  connection.query("select username,uid,delStatus,status_u from user where username=? and password=? ",[req.body.username,req.body.password],(err,result,faild)=>{
   
    if(err) next(err)
    if(result.length){

    }
    if(result.length==0){
      data.code=409
      data.token=''
      data.message='账号或密码错误，请重新输入'
      resp.status(200).json(data)
    }else if(result[0].delStatus!=0){
      data.message='您的账号已被注销，请联系系统管理员进行恢复'
      data.code=409
      data.result=[]
      data.token=''
      resp.status(200).json(data)
    }else if(result[0].status_u!=0){
      data.message='您的账号已被限制登录，请联系系统管理员进行解除'
      data.code=409
      data.result=[]
      data.token=''
      resp.status(200).json(data)
    }else if(result[0].status_u==0&&result[0].delStatus==0){
      data.token=jwtToekn.setJwtToken({username:result[0].username,uid:result[0].uid},tokenPo.secretKey)
      data.code=201
      data.message='登录成功'
      resp.status(200).json(data)
    }
    
  })
})
//用户信息
router.post('/user/info',(req,resp,next)=>{
  let sql=`SELECT username`+''+`,realname,introduction,avatar,role_name,role_remark FROM `+'user'+`,role_u,role WHERE `+'user'+`.uid=role_u.uid AND role_u.role_id=role.role_id AND `+'user'+`.uid=${req.user.uid}`
  connection.query(sql,(err,result,faild)=>{
    if(err) next(err)
    data.result=result
    data.code=200
    data.message='获取成功'
    resp.status(200).json(data)
  })
})
//用户登出
router.post('/user/logout',(req,resp,next)=>{
  data.result=[]
  data.code=201
  data.message='成功登出'
  resp.status(200).json(data)
})
//用户注册 用户注册默认为普通用户
router.put('/user/register',(req,resp,next)=>{
  let sql=`SELECT username FROM `+`user`+` WHERE username='${req.body.username}'`
  connection.query(sql,(err,result,faild)=>{
    if(err)next(err)
    if(result.length>0){
      data.code=409
      data.message='添加失败,用户账号重复'
      data.result=[]
      resp.status(200).json(data)
    }else{
       sql=`INSERT INTO `+`user`+`(username,`+`password`+`,realname,sex,status_u,email,createTIme,introduction,phone,avatar) 	VALUES('${req.body.username}','${req.body.password}','${req.body.realname}','${req.body.sex}','${req.body.status_u}','${req.body.email}',NOW(),'${req.body.introduction}','${req.body.phone}','${req.body.avatar[0]}')`
      connection.query(sql,(err,result,faild)=>{
        if(err)next(err)
        let uid=result.insertId
        let sql=`INSERT INTO role_u(role_id,uid) VALUES(3,${uid})`
        connection.query(sql,(err,result,faild)=>{
          data.code=201
          data.result=[]
          data.message='用户注册成功'
          resp.status(200).json(data)
        })
        //添加之后回显
      })
    }
  })
})
//注销账号功能
router.delete('/user/deleteUser',(req,resp,next)=>{
  if(req.user.uid==1||req.user.username=='admin'){
    data.code=409
    data.message='admin账号禁止注销'
    data.result=[{logout:false}]
    resp.status(200).json(data)
  }else{
    let sql=`UPDATE `+`user`+` SET delStatus=1 WHERE uid=${req.user.uid}`
  connection.query(sql,(err,result,faild)=>{
    if(err) next(err)
    data.code=201
    data.message='账号成功注销'
    data.result=[]
    resp.status(200).json(data)
  })
  }
  
})
module.exports=router